//app.js

App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },

  checkLogin:function(){
    var that = this;
    //1.检查本地是否有3rdsession
    var session = wx.getStorageSync('3rdsession');
    if(session){
      console.log("有session，检查是否过期");
      wx.checkSession({
        success: function(){
          console.log("session未过期，并且在本生命周期一直有效");
          wx.getUserInfo({
            success: function(res){
                that.globalData.userInfo = res.userInfo;
                that.globalData.rdsession = wx.getStorageSync('3rdsession');
                that.globalData.encryptedData = wx.getStorageSync('encryptedData');
                that.globalData.iv = wx.getStorageSync('encryptedData');
                that.globalData.isLogin = true;
            }
          })

        },
        fail: function(){
          console.log("登录态过期,重新进行登录流程");
          that.get3ndSession();
        }
      })
    }else{
      console.log("没有session,需要登录");
      this.get3ndSession();
    }

  },

  get3ndSession:function(){
    var that = this;
    wx.login({
        success: function(res){
          console.log(res);
          var code = res.code;
          wx.getUserInfo({
            success: function(res){
              var encryptedData = res.encryptedData;
              var iv = res.iv;
              var userInfo = res.userInfo;
              var data = {"code":code,"encryptedData":encryptedData,"iv":iv}
              that.request("customer/wx/login", "POST", data, function(res){
                var rdsession = res.result.session;
                wx.setStorageSync('3rdsession', rdsession);
                wx.setStorageSync('encryptedData', encryptedData);
                wx.setStorageSync('iv', iv);
                that.globalData.rdsession = rdsession;
                that.globalData.encryptedData = encryptedData;
                that.globalData.iv = iv;
                that.globalData.isLogin = true;
                that.globalData.userInfo = userInfo;
              });
            },fail : function(){
              console.log("获取用户信息失败");
            }
          })
        },
        fail: function() {
          wx.showModal({
            title: '提示',
            content: '登录失败，部分功能将不可使用',
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              }
            }
          })
        }
      });
  },

  globalData:{
    userInfo:null,
    rdsession:"",
    encryptedData:"",
    iv:"",
    isLogin : false
  },

  request: function(url, method, data, success, fail){
    wx.request({
      url: 'https://www.86xcz.cn/'+url,
      method: method,
      data: data == null ? {} : data,
      header: {
          'content-type': 'application/json',
          '3rdsession' : this.globalData.rdsession,
          "encryptedData": this.globalData.encryptedData,
          "iv": this.globalData.iv
      },
      success: function(res) {
        console.log(res);
        if(res.data.code == 0){
          success(res.data);
        }else{
          if(fail != null){
            fail(res.data);
          }
        }
      },
      fail : function(res) {
          wx.showModal({
          title: "很抱歉",
          content: "请求失败",
          showCancel : false
        })
      }
    })
  },

  uploadFile : function(img_path, success){
    var that = this;
    console.log("img_path:"+img_path);
    wx.uploadFile({
      url: 'https://www.86xcz.cn/upload/task/process',
      filePath : img_path,
      name:'file',
      header: {
          'content-type': 'application/json',
          '3rdsession' : that.globalData.rdsession,
          "encryptedData": that.globalData.encryptedData,
          "iv": that.globalData.iv
      },
      success: function(res){
        console.log(res);
        res.data = JSON.parse(res.data);
        if(res.data.code == 0){
          success(res);
        }else{
          that.closeLoading();
          that.showAlert("请求失败", res.data.msg);
        }
      },
      fail: function(res) {
        that.showAlert("文件上传失败,请稍后重试");
      }
    })
  },

  showAlert : function(title, content, success){
      wx.showModal({
          title: title,
          content: content,
          showCancel: false,
          success : success
        })
  },

  showLoading : function(title){
    wx.showToast({
      title: title == null ? "加载中..." : title, 
      icon: 'loading',
      duration: 10000
    })
  },

  closeLoading : function(){
    wx.hideToast();
  },

  openMap : function(lat, lng, name, address){
    console.log("openMap:"+lat+","+lng);
    wx.openLocation({
      latitude: lat,
      longitude: lng,
      name: name,
      address: address,
      scale: 28
    })
    // wx.getLocation({
    //       type: 'gcj02', //返回可以用于wx.openLocation的经纬度
    //       success: function(res) {
    //           wx.openLocation({
    //           latitude: lat,
    //           longitude: lng,
    //           name: name,
    //           address: address,
    //           scale: 28
    //           })
    //       }
    //   })
  }

})